package manongfanshen.cas.demo01;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author ChangLiang
 * @date 2020/4/24
 */
public class SequenceV2 implements Isequence{

    private AtomicInteger value = new AtomicInteger(0);

    @Override
    public int next() {
        while (true) {
            int current = value.get();
            int next = current+1;
            if (value.compareAndSet(current, next)) {
                return next;
            }
        }
    }

    @Override
    public int getValue() {
        return value.intValue();
    }
}
